package edu.usc.epigenome.dnaase.script;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;


public class vcfConcordanceWithListerData {

	final private static String USAGE = "vcfConcordance [opts] sample vcfFileName1 ListerDir ...";
	/**
	 * @param args
	 */
	//@Option(name="-mc",usage="compare with methylated C in lister")
	//protected boolean samtools = false;
	//@Option(name="-verbose",usage="output additional infor")
	//protected boolean verbose = false;
	@Option(name="-chrom",usage="chromsome")
	protected List<String> chrs = new ArrayList<String>();
	
	@Argument
	private List<String> stringArgs = new ArrayList<String>();
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		new vcfConcordanceWithListerData().doMain(args);
	}

	public void doMain(String[] args) throws IOException{
		CmdLineParser parser = new CmdLineParser(this);
		// if you have a wider console, you could increase the value;
		// here 80 is also the default
		parser.setUsageWidth(80);
		try
		{
			parser.parseArgument(args);
			if (stringArgs.size() < 3) throw new CmdLineException(USAGE);
		
		}
		catch (CmdLineException e)
		{
			System.err.println(e.getMessage());
			// print the list of available options
			parser.printUsage(System.err);
			System.err.println();
			return;
		}

		String sampleName = stringArgs.remove(0);
		String vcfFileName = stringArgs.remove(0);
		String listerFilePath = stringArgs.remove(0);
		HashMap<String,String> vcfmap = new HashMap<String,String>();
		//HashMap<String,String> listerMap = new HashMap<String,String>();
		BufferedReader br = new BufferedReader(new FileReader(vcfFileName));
		//int hetConcordance = 0;
		int nonRefNomCvcf = 0;
		int het1_hom2 = 0;
		//int hom1_het2 = 0;
		int het1 = 0;
		int homC_Overlap_nonRefvcf = 0;
		//int het2 = 0;
		int totalCount1 = 0;
		int totalCount2 = 0;
		
		//int hetConcordanceKnownSnp = 0;
		//int het1_hom2KnownSnp = 0;
		//int hom1_het2KnownSnp = 0;
		//int het1KnownSnp = 0;
		//int het2KnownSnp = 0;
		//int totalCount1KnownSnp = 0;
		//int totalCount2KnownSnp = 0;
		
		String line;
		while( (line = br.readLine()) != null){
			if(line.startsWith("#"))
				continue;
			
			String[] tmpArray = line.split("\t");
			if(!tmpArray[6].startsWith("PASS"))
				continue;
			//boolean knownSNP = false;
			//if(tmpArray[2].startsWith("rs"))
				//knownSNP = true;
			String key = tmpArray[0] + "-" + tmpArray[1];
			String[] attributes = tmpArray[7].split(";");
			String word = null;
			String word1 = tmpArray[3] + "~" + tmpArray[4];
			String word2 = null;
			String word3 = null;
			for(String attribute : attributes){
				
					if(attribute.matches("AF=\\S+")){
						String[] temp = attribute.split("=");
						String[] tempAF = temp[1].split(",");
						word2 = tempAF[0];
					}
				
			}
			if(word2.equalsIgnoreCase("0.50"))
				het1++;
			
			if(tmpArray[9].startsWith("0/1")){
				word3 = "0/1";
				
					
			}
			else if(tmpArray[9].startsWith("1/1")){
				word3 = "1/1";
				if(word2.equalsIgnoreCase("1.00") &&  (tmpArray[4].equalsIgnoreCase("C") || tmpArray[4].equalsIgnoreCase("G")))
					nonRefNomCvcf++;
				
			}
			word = word1 + "~" + word2 + "~" + word3;
			vcfmap.put(key, word);
			totalCount1++;
			//if(knownSNP)
				//totalCount1KnownSnp++;
		}
		br.close();
		for(String chr : chrs){
			String cytosineFileName = chr + "_" + sampleName + "_c_basecalls.txt"; 
			File cytosineFile = new File(listerFilePath,cytosineFileName);
			BufferedReader br2 = new BufferedReader(new FileReader(cytosineFile));
			 while( (line = br2.readLine()) != null){
				 String[] tmpArray = line.split("\t");
				 if(Integer.parseInt(tmpArray[6]) == 0)
					 continue;
				 String key = chr + "-" + tmpArray[1];
				 if(vcfmap.containsKey(key)){
					 String value = vcfmap.get(key);
					 String[] tmpValue = value.split("~");
					 if(tmpValue[2].equalsIgnoreCase("0.50")){
						 het1_hom2++;
					 }
					 if(tmpValue[2].equalsIgnoreCase("1.00") && tmpValue[3].equalsIgnoreCase("1/1") && (tmpArray[1].equalsIgnoreCase("C") || tmpArray[1].equalsIgnoreCase("G"))){
						 homC_Overlap_nonRefvcf++;
					 }
					 
					
				 }
				 
				 totalCount2++;
				 String listerKey = chr + "-" + tmpArray[1];
				// listerMap.put(listerKey, tmpArray[3]);
			 }
			 br2.close();
		}
		/*
		Iterator<String> It = vcfmap.keySet().iterator();
		while(It.hasNext()){
			String key = It.next();
			if(listerMap.containsKey(key)){
				String value = vcfmap.get(key);
				 String[] tmpValue = value.split("~");
				 if(tmpValue[3].equalsIgnoreCase("1/1")){
						if(tmpValue[2].equalsIgnoreCase("1.00") &&  (tmpValue[1].equalsIgnoreCase("C") || tmpValue[1].equalsIgnoreCase("G"))){
							homC_unique1++;
						}
				 }
			}
		}*/
		
		System.out.println("totalCount1: " + totalCount1);
		System.out.println("het1: " + het1);
		System.out.println("totalCount2: " + totalCount2);
		System.out.println(" homC_Overlap_nonRefvcf: " +  homC_Overlap_nonRefvcf);
		//System.out.println("hetConcordance: " + hetConcordance);
		System.out.println("het1_hom2: " + het1_hom2);
		//System.out.println("hom1_het2: " + hom1_het2);
		
		//System.out.println("totalCount1KnownSnp: " + totalCount1KnownSnp);
		//System.out.println("het1KnownSnp: " + het1KnownSnp);
		//System.out.println("totalCount2KnownSnp: " + totalCount2KnownSnp);
		//System.out.println("het2KnownSnp: " + het2KnownSnp);
		//System.out.println("hetConcordanceKnownSnp: " + hetConcordanceKnownSnp);
		//System.out.println("het1_hom2KnownSnp: " + het1_hom2KnownSnp);
		//System.out.println("hom1_het2KnownSnp: " + hom1_het2KnownSnp);
	}
}
